package com.aidex.biz.service.impl;

import com.aidex.biz.domain.BizAnhuanCompany;
import com.aidex.biz.domain.BizAnhuanZicanList;
import com.aidex.biz.service.AnHuanProjectApiService;
import com.aidex.biz.service.BizAnhuanCompanyService;
import com.aidex.biz.service.BizAnhuanZicanDetailsService;
import com.aidex.biz.service.BizAnhuanZicanListService;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.utils.http.HttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 安环项目接口服务实现类
 */
@Service
public class AnHuanProjectApiServieImpl implements AnHuanProjectApiService {
    private static final String BASE_URL = "https://ctjtsafety.yichangct.com";
    private static final String username = "meiqin";
    private static final String password = "WW5kKcwwL4";
    private static final String token = "Bearer eyJhbGciOiJIUzUxMiJ9" +
            ".eyJsb2dpbl91c2VyX2tleSI6ImY4ODlkNGM5LTZlMzctNDQ3Ni1iNTVmLWYzMTU1MTBjYjU5MiJ9" +
            ".69yDUMp5WJasy5coxOVnEiUYvx_9QhUrDFvdTXkw8XhjsMd9LQJGDQX48MGua_pFGYpLqPxYvLPlJ5AWQQ-JbQ";

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BizAnhuanCompanyService bizAnhuanCompanyService;

    @Autowired
    private BizAnhuanZicanDetailsService bizAnhuanZicanDetailsService;

    @Autowired
    private BizAnhuanZicanListService bizAnhuanZicanListService;


    /**
     * 获取token
     *
     * @return
     */
    @Override
    public String getToken() {
        String token = redisCache.getStringValue("anHuan-token");
        if (token != null) {
            return token;
        } else {
            JSONObject params = new JSONObject();
            params.put("username", username);
            params.put("password", password);
            String result = HttpUtils.sendPost(BASE_URL + "/ct-safetyManagement-api/getToken", params.toJSONString());
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (null != jsonObject) {
                if (jsonObject.get("code").equals(200)) {
                    token = jsonObject.getString("token");
                    redisCache.set("anHuan-token", token, 30 * 60 * 1000);
                    return token;
                }
            }
        }
        return null;
    }

    /**
     * 获取公司考核排名
     *
     * @return
     */
    @Override
    public JSONObject getCompanyPaiMing() {
        String token = getToken();
        if (null != token) {
            String result = HttpUtils.sendGet(BASE_URL + "/ct-safetyManagement-api/dock/getComScore", null, token,
                    true);
            if (null != result) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (null != jsonObject) {
                    if (jsonObject.get("code").equals(200)) {
                        bizAnhuanCompanyService.deleteBizAnhuanCompanyByIds(null);
                        JSONArray data = jsonObject.getJSONArray("data");
                        for (int i = 0; i < data.size(); i++) {
                            JSONObject jsonObject1 = data.getJSONObject(i);
                            BizAnhuanCompany bizAnhuanCompany = new BizAnhuanCompany();
                            bizAnhuanCompany.setComname(jsonObject1.getString("comName"));
                            bizAnhuanCompany.setCycle(jsonObject1.getString("cycle"));
                            bizAnhuanCompany.setScore(jsonObject1.getBigDecimal("score"));
                            bizAnhuanCompany.setLastscore(jsonObject1.getBigDecimal("lastScore"));
                            bizAnhuanCompany.setAvgscore(jsonObject1.getBigDecimal("avgScore"));
                            bizAnhuanCompany.setFinalscore(jsonObject1.getBigDecimal("finalScore"));
                            bizAnhuanCompanyService.save(bizAnhuanCompany);
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取资产列表
     *
     * @return
     */
    @Override
    public JSONObject getZiChanList() {
        String token = getToken();
        if (null != token) {
            String result = HttpUtils.sendGet(BASE_URL + "/ct-safetyManagement-api/dock/assetList", null, token, true);
            if (null != result) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (null != jsonObject) {
                    if (jsonObject.get("code").equals(200)) {
                        bizAnhuanZicanListService.deleteBizAnhuanZicanListByIds(null);
                        JSONArray data = jsonObject.getJSONArray("data");
                        for (int i = 0; i < data.size(); i++) {
                            JSONObject jsonObject1 = data.getJSONObject(i);
                            BizAnhuanZicanList bizAnhuanZicanList = new BizAnhuanZicanList();
                            bizAnhuanZicanList.setIsNewRecord();
                            bizAnhuanZicanList.setId(jsonObject1.getString("id"));
                            bizAnhuanZicanList.setName(jsonObject1.getString("name"));
                            bizAnhuanZicanList.setAssetvalue(jsonObject1.getString("assetValue"));
                            bizAnhuanZicanList.setType(jsonObject1.getString("type"));
                            bizAnhuanZicanList.setStatus(jsonObject1.getString("status"));
                            bizAnhuanZicanList.setFgbmleaderStr(jsonObject1.getString("fgbmLeader_str"));
                            bizAnhuanZicanList.setDeptidStr(jsonObject1.getString("deptId_str"));
                            bizAnhuanZicanListService.save(bizAnhuanZicanList);
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取资产详情
     *
     * @param id
     * @return
     */
    @Override
    public JSONObject getZiChanDetail(String id) {
        String token = getToken();
        if (null != token) {
            String result = HttpUtils.sendGet(BASE_URL + "/ct-safetyManagement-api/dock/assetDetail", "id=" + id,
                    token, true);
            if (null != result) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (null != jsonObject) {
                    if (jsonObject.get("code").equals(200)) {
                        return jsonObject;
                    }
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String result = HttpUtils.sendGet(BASE_URL + "/ct-safetyManagement-api/dock/getComScore", null, token, true);
        System.out.println(result);
    }
}
